home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Business Master (3rd Edition)
/
The Business Master (3rd Edition).iso
/
files
/
utilreen
/
txt2ps
/
psprint.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-01-23
|
10KB
|
299 lines
/*+
Name: psprint.c
Date: 26-Jan-1989
Author: Kent J. Quirk
Totel Systems, Inc.
489 Groton Road
Westford, MA 01886
(508) 692-9535
BIX and MCI Mail: kquirk
Abstract: This program provides a way to list text files on a PostScript
printer. It works by enclosing and quoting each line of
the file and sending it to the printer as a PostScript
command. Many parameters are variable.
Usage: This program is a filter; that is, it takes input from stdin
and writes its output to stdout. There are a number of
switches supported (defaults in parentheses).
-m - set left margin (in points) (24)
-t - set top margin (in points) (24)
-l - set leading (points between lines) (11.5)
-p - set pointsize of font (10)
-f - set font name (Courier)
-n - set number of lines per page (66)
-i - ignore form feeds
-? - print a help message
All the defaults (except page length) are set in the file
PSPRINT.PS, which must be in the same directory as this
program. If you don't use DOS 3.0 or later, it probably has
to be in the current directory.
In fact, the file is not searched for by that name, but by
the same name as the program and with the extension .PS.
If you need to rename this, you can do it; just rename the
.PS file as well.
PSPRINT pays attention to control-L characters as long as they
occur as the first character on a line. I've never seen a
program that didn't print them there, so I think this is safe
(but it's lazy).
History: 26-Jan-89 - Kent J. Quirk, Totel Systems, Inc.
Original Version
3-3-89 - Tim Frost, Roundhill Computer Systems Limited
The program will now process file names (including wildcards
if linked with the C compiler wildcard function) if specified
after the flags on the command line. If no file names are
present, it still uses standard input. If filenames are
processed, it ignores standard input. LINK WITH ALTERNATE
SETARGV (MSC) or WILDARGS (TC2) MODULE TO GET WILDCARD
SUPPORT ON COMMAND LINE.
If the PSPOUT envirpnment variable is set, the program uses
its value as a file to write to instead of standard output.
(e.g. "SET PSPOUT=LPT2").
3-5-89 - kjq
Added -i (ignore form feeds) switch. Cleaned up dofile() to
accomodate it. Linked with setargv.obj and rereleased.
Bugs: It can't print sideways yet. There are PostScript programs
(see the Adobe book) which will take this output and print it
in half-page form.
Form Feeds should be handled anywhere on a line.
Tabs should be optionally expandable.
It might be nice to emit a trailer that conforms with the
Adobe style spec defining fonts used and number of pages.
-*/
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 250
int pagelen = 66;
int ignoreff = 0;
int filedone = 0;
/**** p s q u o t e ****
Abstract: This routine takes a string and "quotes" it for PostScript.
That is, it surrounds it in parens and escapes any character
less than a space, as well as parens and backslash.
Parameters: The input string
Returns: A pointer to a PostScript version of the input string.
Comments: The output string is limited to 250 characters.
The output string is a static buf used on every call to
this routine. If you want to keep the string around,
copy it to your own area or do a strdup() to duplicate it.
****************************/
char *psquote(char *s)
{
static char buf[BUFSIZE];
int i = 0;
buf[i++] = '(';
for (; i<BUFSIZE-2; i++, s++)
{
if (*s)
{
if ((*s < ' ') || (*s > '\x7e'))
{
sprintf(buf+i, "\\%3.3o", *s);
i+=3;
}
else
{
switch (*s)
{
case '(':
case ')':
case '\\':
buf[i++] = '\\'; /* no break */
default:
buf[i] = *s;
}
}
}
else
break;
}
if (i == 1) /* no characters added */
buf[i++] = ' '; /* so add a space to satisfy show */
buf[i++] = ')'; /* then close it and terminate it */
buf[i++] = 0;
return(buf);
}
/**** u s a g e ****
Prints a usage message to stderr and quits.
****************************/
void usage(void)
{
fprintf(stderr, "PSPRINT -[mtlpfn] [file ... ]\n");
fprintf(stderr, "This program reads each named file (or standard input, if\n");
fprintf(stderr, "no files specified) and sends it to the standard output\n");
fprintf(stderr, "(or to the file named in the PSPOUT environment variable,\n");
fprintf(stderr, "if defined) as a PostScript print file. Options are:\n");
fprintf(stderr, " -m - set left margin (in points) (24)\n");
fprintf(stderr, " -t - set top margin (in points) (24)\n");
fprintf(stderr, " -l - set leading (points between lines) (11.5)\n");
fprintf(stderr, " -p - set pointsize of font (10)\n");
fprintf(stderr, " -f - set font name (Courier)\n");
fprintf(stderr, " -n - set number of lines per page (66)\n");
fprintf(stderr, " -i - ignore form feeds (deletes them)\n");
fprintf(stderr, " -? - print this message\n");
exit(1);
}
/**** d o f i l e ****
Processes one input file.
****************************/
void dofile(void)
{
char buf[BUFSIZE];
char *bp;
int newpage = 0;
register int i;
for (i=1; bp = gets(buf); i++) /* for every line in input file */
{
if (*bp == '\x0C') /* forced new page */
{
bp++;
if (!ignoreff)
newpage = 1;
}
else if ((filedone) || (i % pagelen == 0)) /* calculated new page */
{
filedone = 0;
newpage = 1;
}
if (newpage)
{
printf("sp ");
i = 1;
newpage = 0;
}
printf("%s nl\n", psquote(bp));
}
}
/**** m a i n ****
The main routine that does all the work. Opens the PSPRINT.PS file,
emits its contents up to the %%EndProlog message, emits any changed
parameters, then finishes PSPRINT.PS. Finally, it sends the quoted
input file.
****************************/
main(int argc, char *argv[])
{
char buf[BUFSIZE];
char *p;
FILE *f;
char * parg;
char * pspout;
if ((pspout = getenv("PSPOUT")) != NULL)
{
int outdev;
outdev = open(pspout,O_TEXT|O_WRONLY);
if (outdev < 0)
{
fprintf(stderr,"Unable to open output file %s",pspout);
exit(1);
}
dup2(outdev,1);
}
if (argv[1][1] == '?')
usage();
if ((argc == 1) && (isatty(0))) /* no args, stdin not redirected */
usage();
strcpy(buf, argv[0]);
if ((p = strchr(buf, '.')) != NULL)
*p = 0;
strcat(buf, ".ps"); /* open file with this name but .ps ext */
if ((f = fopen(buf, "r")) == NULL)
{
fprintf(stderr, "Unable to open PostScript header file '%s'\n",
buf);
return(1);
}
while (fgets(buf, BUFSIZE, f) != NULL) /* while more file */
{
if (strncmp(buf, "%%EndProlog", 10) == 0) /* copy to EndProlog */
{
while ((parg = *++argv) != NULL)
{
if (*parg == '-')
{
switch (*(parg + 1))
{
case 'm':
printf("/lmarg %s def\n", parg+2);
break;
case 't':
printf("/tmarg %s def\n", parg+2);
break;
case 'l':
printf("/leading %s def\n", parg+2);
break;
case 'p':
printf("/pointsize %s def\n", parg+2);
break;
case 'f':
printf("/font /%s def\n", parg+2);
break;
case 'n':
pagelen = atoi(parg+2);
break;
case 'i':
ignoreff = 1;
break;
default:
fprintf(stderr, "Don't know what to do with '%s'\n",
parg);
break;
}
}
else break;
}
}
printf("%s", buf);
}
fclose(f);
while ((parg = *argv++) != NULL)
{
int infile;
infile = open(parg,O_TEXT|O_RDONLY);
if (infile < 0)
{
fprintf(stderr, "Unable to open input file %s\n",parg);
exit(1);
}
dup2(infile,0);
dofile();
filedone++;
}
if (!filedone) dofile();
printf("lp\n"); /* show the last page */
return(0);
}